package cn.ydw.community.service.impl;

import cn.ydw.community.entity.Message;
import cn.ydw.community.dao.MessageDao;
import cn.ydw.community.service.MessageService;
import cn.ydw.community.util.SensitiveWordsFilter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dw
 * @since 2020-10-18
 */
@Service
public class MessageServiceImp extends ServiceImpl<MessageDao, Message> implements MessageService {


    @Autowired
    private MessageDao messageDao;

    @Autowired
    private SensitiveWordsFilter sensitiveWordsFilter;
    /**
     * 查询用户对话
     *
     * @param page
     * @param userId
     * @return
     */
    @Override
    public List<Message> findConversations(Page page, int userId) {
        return messageDao.selectConversations(page, userId);
    }

    /**
     * 查询私信消息，统计两用户之间私信数量
     *
     * @param conversationId
     * @param page
     * @return
     */
    @Override
    public List<Message> findLetters(String conversationId, Page page) {
        return messageDao.selectLetters(page, conversationId);
    }

    /**
     * 统计未读消息
     *
     * @param userId
     * @param conversationId
     * @return
     */
    @Override
    public int findLetterUnreadCount(int userId, String conversationId) {
        return messageDao.selectLetterUnreadCount(userId, conversationId);
    }

    /**
     * 更新消息状态
     *
     * @param message
     */
    @Override
    public int addMessage(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveWordsFilter.filter(message.getContent()));
        return messageDao.insertMessage(message);
    }

    /**
     * 插入消息
     *
     * @param ids
     */
    @Override
    public int readMessage(List<Integer> ids) {
        return messageDao.updateStatus(ids, 1);
    }

    /**
     * 查看最新通知消息
     *
     * @param userId
     * @param topic
     */
    @Override
    public Message findLatestNotice(int userId, String topic) {
        return messageDao.selectLatestNotice(userId, topic);
    }

    /**
     * 查看通知消息数量
     *
     * @param userId
     * @param topic
     */
    @Override
    public int findNoticeCount(int userId, String topic) {
        return messageDao.selectNoticeCount(userId, topic);
    }

    /**
     * 查看未读通知消息数量
     *
     * @param userId
     * @param topic
     */
    @Override
    public int findNoticeUnreadCount(int userId, String topic) {
        return messageDao.selectNoticeUnreadCount(userId, topic);
    }

    /**
     * 查看通知消息列表
     */
    @Override
    public List<Message> findNotices(Page page, int userId, String topic) {
        return messageDao.selectNotices(page, userId, topic);
    }
}
